<template>
  <div class="home">
    <div class="task">
      <strong>任务列表</strong>
    </div>
    <div class="list">
      <ul>
        <li
          :class="{ delLine: checkList[index] }"
          v-for="(item, index) in list"
          :key="index"
        >
          <input type="checkbox" v-model="checkList[index]" />
          <span v-if="index != inpIndex" @click="editInp(index)">
            {{ item }}
          </span>
          <span v-else="index == inpIndex">
            <input
              type="text"
              v-model="editVal"
              @keyup.enter="editOK(index)"
              @blur="editOK(index)"
            />
          </span>
        </li>
      </ul>
    </div>
    <div class="taskTotal">
      任务总数 {{ total }}；还有 {{ isFinish }} 未完成；【<a
        href="#"
        @click.prevent="finish"
        >完成</a
      >】
    </div>
    <div class="inp">
      <input type="text" placeholder="请输入任务名称" v-model="inpVal" /><button
        @click="add"
      >
        添加
      </button>
    </div>
  </div>
</template>

<script>
export default {
  name: "HomeView",
  data() {
    return {
      list: ["预习Vuex知识点", "编写课后练习题", "抄写单词20遍"],
      // 存储复选框
      checkList: [],
      // 输入框的值
      inpVal: "",
      // 二次输入的值
      editVal: "",
      // 输入框索引
      inpIndex: -1,
    };
  },
  created() {
    this.checkList = this.list.map(() => false);
  },
  methods: {
    add() {
      if (this.inpVal == "") {
        alert("请输入任务名称");
        return;
      }
      this.list.push(this.inpVal);
      this.checkList.push(false);
      this.inpVal = "";
    },
    finish() {
      this.list = this.list.filter((item, index) => {
        return !this.checkList[index];
      });
      this.checkList = this.checkList.filter((item, index) => {
        return !this.checkList[index];
      });
    },
    editInp(idx) {
      this.inpIndex = idx;
      this.editVal = this.list[idx];
    },
    editOK(index) {
      this.inpIndex = -1;
      this.list[index] = this.editVal;
    },
  },
  computed: {
    total() {
      return this.list.length;
    },
    isFinish() {
      return this.checkList.reduce((pre, cur) => {
        return cur ? (pre-- == 0 ? 0 : pre) : pre;
      }, this.list.length);
    },
  },
};
</script>
<style lang="less" scoped>
.home {
  position: absolute;
  width: 400px;
  border: 1px solid #000;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  color: #444;
  .task {
    height: 50px;
    font-size: 30px;
    font-weight: 700;
    text-align: center;
    line-height: 50px;
    color: #000;
  }
  .list {
    padding: 10px;
    ul {
      li {
        height: 20px;
        margin: 5px 0;
        line-height: 20px;
        border-bottom: 1px solid #888;
        padding-bottom: 9px;
        &.delLine {
          color: #ccc;
          text-decoration: line-through;
        }
      }
    }
  }
  .taskTotal {
    padding: 0 0 10px 10px;
  }
  .inp {
    height: 40px;
    margin: 10px 0 0 10px;
    input {
      height: 30px;
      border: 1px solid #666;
      outline: none;
      box-sizing: border-box;
      vertical-align: middle;
    }
    button {
      height: 30px;
      vertical-align: middle;
    }
  }
}
</style>
